<template>
  <d2-container>
    <div class="d2-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>公告管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" class="SearchForm">
      <el-form-item>
        <router-link to="notes/add">
          <el-button type="success">新增公告</el-button>
        </router-link>
      </el-form-item>
    </el-form>
    <el-table border :data="tableData.list">
      <el-table-column
        label="ID"
        
        align="center"
        prop="_id"
      ></el-table-column>
      <el-table-column
        label="公告标题"
        
        align="center"
        prop="title"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        
        align="center"
        prop="create_time"
      >
      <template slot-scope="scope">
        {{$moment(scope.row.create_time).format('YYYY-MM-DD hh:mm:ss')}}
      </template>
      </el-table-column>
      <el-table-column
        label="开启时间"
        
        align="center"
        prop="time"
      >
      <template slot-scope="scope">
        {{$moment(scope.row.time).format('YYYY-MM-DD hh:mm:ss')}}
      </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :disabled="true"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <router-link :to="`/notes/edit/${scope.row._id}`">
            <el-button type="text">详情/编辑</el-button>
          </router-link>
          <el-button
            type="text"
            class="margin-left"
            @click="deleteInfo(scope.row._id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="search"
      @current-change="search"
      :current-page.sync="page"
      :page-sizes="[10, 25, 50, 100]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.count"
    ></el-pagination>
  </d2-container>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      searchForm: {
        user_name: "",
      },
      tableData: {
        list: [],
        count: 0,
      },
      page: 1,
      pageSize: 10,
    };
  },
  methods: {
    search() {
      let params = {
        page: this.page,
        page_size: this.pageSize,
        ...this.searchForm,
      };

      this.$axios.post("/admin/getNoteList", params).then((result) => {
        if (result) {
          this.tableData = result;
        } else {
          this.$message.error(result.data.msg);
        }
      });
    },
    reset() {
      this.searchForm = {};
      this.search();
    },
    deleteInfo(id) {
      const _this = this;
      this.$confirm("确定删除该条数据？", "确认删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnPressEscape: false,
        closeOnClickModal: false,
      }).then(() => {
        let params = {
          _id: id,
        };
        _this.$axios.post("/admin/deleteNote", params).then((res) => {
          if (res) {
            _this.$message({
              message: res.data.msg,
              type: "error",
            });
          } else {
            _this.$message({
              message: "删除成功",
              type: "success",
            });
            _this.search();
          }
        });
      });
    },
  },
  created() {
    this.search();
  },
};
</script>

<style scoped lang="scss">
</style>
